#include <algorithm>
#include <vector>
using namespace std;
struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

/*
 * @lc app=leetcode.cn id=113 lang=cpp
 *
 * [113] 路径总和 II
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<vector<int>> res;
    vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
        vector<int> temp;
        dfs(root, targetSum, temp);
        return res;
    }

    void dfs(TreeNode* root, int targetSum, vector<int>& now) {
        if (!root) return;
        now.push_back(root->val);
        if (root->val == targetSum && !root->left && !root->right) {
            res.push_back(now);
            now.pop_back();
            return;
        }
        // if (root->val == targetSum && (root->left || root->right)) {
        //     now.pop_back();
        //     return;
        // }
        dfs(root->left, targetSum - root->val, now);
        dfs(root->right, targetSum - root->val, now);
        now.pop_back();
    }
};
// @lc code=end

